/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hojat6;

import structure5.BinaryTree;

/**
 *
 * @author Juan Pablo
 */
public class huffmanTree implements Comparable<huffmanTree>
{
    BinaryTree<node> empty;
    BinaryTree<node> root; // root of tree
    int totalWeight;     // weight of tree

    public huffmanTree(node e)
    // post: construct a node with associated character
    {
        empty = new BinaryTree<node>();
        root = new BinaryTree<node>(e,empty,empty);
        totalWeight = e.frequency;
    }

    public huffmanTree(huffmanTree left, huffmanTree right)
    // pre: left and right non-null
    // post: merge two trees together and total weights
    {
        this.totalWeight = left.totalWeight + right.totalWeight;
        root = new BinaryTree<node>(new node(this.totalWeight),left.root,right.root);
    }

    public int compareTo(huffmanTree other)
    // pre: other is not null
    // post: return integer reflecting relation between values
    {
        huffmanTree that = (huffmanTree)other;
        return this.totalWeight - that.totalWeight;
    }

    public boolean equals(Object that)
    // post: return true if this and that are same tree instance
    {
        return this == that;
    }
    
    public void print()
    // post: print out strings associated with characters in tree
    {
        print(this.root,"");
    }

    protected void print(BinaryTree<node> r, String representation)
    // post: print out strings associated with chars in tree r,
    //       prefixed by representation
    {
        if (!r.left().isEmpty())
        {   // interior node
            print(r.left(),representation+"0"); // append a 0
            print(r.right(),representation+"1"); // append a 1
        } else { // node; print encoding
            node e = r.value();
            System.out.println("El código de la letra "+e.ch+" es "+
               representation+" (Frecuencia: "+e.frequency+")");
        }
    }
    
    public char obtenerIzquierda(){
        return root.value().ch;
    }
    
    public char obtenerDerecha(){
        return root.value().ch;
    }
    
    public boolean vacio(){
        return root.isEmpty();
    }
    
    public boolean isLeft(){
        return root.isLeftChild();
    }
    
    public boolean isRight(){
        return root.isRightChild();
    }
    
    public void geValor(){
        
    }

}

/*
If a woodchuck could chuck wood!
*/
/*
    Encoding of ! is 0000 (frequency was 1)
    Encoding of a is 00010 (frequency was 1)
    Encoding of l is 00011 (frequency was 1)
    Encoding of u is 001 (frequency was 3)
    Encoding of d is 010 (frequency was 3)
    Encoding of k is 0110 (frequency was 2)
    Encoding of w is 0111 (frequency was 2)
    Encoding of I is 10000 (frequency was 1)
    Encoding of f is 10001 (frequency was 1)
    Encoding of h is 1001 (frequency was 2)
    Encoding of c is 101 (frequency was 5)
    Encoding of   is 110 (frequency was 5)
    Encoding of o is 111 (frequency was 5)
    Old length=256 new length=111 57% compression.
*/

